package com.feidian.helper.util;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;

import com.feidian.helper.R;

public class NetUtils {
	private static final String TAG = "NetUtils";

	public static boolean isWifiAvailable(Context context) {
		ConnectivityManager mConnMgr = (ConnectivityManager) context
				.getSystemService(Context.CONNECTIVITY_SERVICE);
		NetworkInfo mWifi = mConnMgr
				.getNetworkInfo(ConnectivityManager.TYPE_WIFI);

		boolean flag = false;
		if ((mWifi != null) && mWifi.isAvailable()) {
			flag = mWifi.isConnected();
		}
		return flag;
	}

	public static boolean isMobileNetWorkAvailable(Context context) {
		ConnectivityManager mConnMgr = (ConnectivityManager) context
				.getSystemService(Context.CONNECTIVITY_SERVICE);
		NetworkInfo mMobile = mConnMgr
				.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
		boolean flag = false;
		if (mMobile != null && mMobile.isAvailable()) {
			flag = mMobile.isConnected();
		}
		return flag;
	}

	public static boolean isConnectionAvailable(Context cotext) {
		boolean available = false;
		ConnectivityManager mConnMgr = (ConnectivityManager) cotext
				.getSystemService(Context.CONNECTIVITY_SERVICE);
		NetworkInfo activeNetworkInfo = mConnMgr.getActiveNetworkInfo();
		if (activeNetworkInfo != null && activeNetworkInfo.isConnected()) {
			available = true;
		}
		return available;
	}

	public static String readString(Context context) {
		StringBuffer buffer = new StringBuffer();
		BufferedReader reader = null;
		try {
			String line;
			reader = null;//new BufferedReader(new InputStreamReader(context
					//.getResources().openRawResource(R.raw.agreement)));
			while ((line = reader.readLine()) != null) {
				buffer.append(line+"\n");
			}
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			try {
				if (reader != null) {
					reader.close();
				}
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		return buffer.toString();

	}
}
